<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of orderinfomodel
 *
 * @author lucky
 */
include_once APPPATH . "core/base_model.php";

class ordermodel extends base_model {

    private static $table = 'order_info';
    const JD_CHANNEL_ID = 2;  //京东
    const TAOBAO_CHANNEL_ID = 1; //淘宝
    const B2C_CHANNEL_ID = 3; //b2c
    
    public function __construct() {
        parent::__construct();
        $this->load->database('apach');
    }

    /**
     * 新增 订单
     *
     * @param $data
     *
     * @return null
     */
    public function newHjOrderInfo($seller_id,$data) {
        if (!$data || !is_array($data)) {
            return null;
        }
        $chdata['order_id'] = $data['order_no'];
        $chdata['status'] = isset($data['status']) ? $data['status'] : "";
        $chdata['created'] = isset($data['created']) ? $data['created'] : "";
        $chdata['pay_time'] = isset($data['paid_time']) ? $data['paid_time'] : "";
        $chdata['buyer_nick'] = isset($data['username']) ? $data['username'] : "";
        $chdata['total_fee'] = isset($data['price']) ? ($data['price']/100) : "";
        $chdata['payment'] = isset($data['paid_price']) ? ($data['paid_price']/100) : "";
        $chdata['seller_memo'] = isset($data['remark']) ? $data['remark'] : "";
        $chdata['receiver_name'] = isset($data['consignee_name']) ? $data['consignee_name'] : '';
        $chdata['receiver_province'] = isset($data['province_name']) ? $data['province_name'] : '';
        $chdata['receiver_city'] = isset($data['city_name']) ? $data['city_name'] : '';
        $chdata['receiver_town'] = isset($data['area_name']) ? $data['area_name'] : '';
        $chdata['receiver_address'] = isset($data['consignee_address']) ? $data['consignee_address'] : '';
        $chdata['receiver_mobile'] = isset($data['consignee_phone']) ? $data['consignee_phone'] : '';
        $chdata['seller_id'] = $seller_id;
        if (isset($data['deadline'])) {
            $chdata['deadline'] = $data['deadline'];
        }
        $chdata['channel_id'] = ordermodel::B2C_CHANNEL_ID; //京东渠道
        switch ($chdata['status']) {
            case "1":
                $chdata['status'] = 1; //等待发货
                break;
        }

        $est = $this->getOrders($chdata['order_id']);
        if ($est) {
            $this->db->where('order_id', $chdata['order_id']);
            return $this->update(self::$table, $chdata);
        } else {
            return $this->insert(self::$table, $chdata);
        }
    }

    /**
     * 新增 订单
     *
     * @param $data
     *
     * @return null
     */
    public function newjdOrderInfo($data) {
        if (!$data || !is_array($data)) {
            return null;
        }
        $chdata['order_id'] = $data['order_id'];
        $chdata['status'] = isset($data['order_state']) ? $data['order_state'] : "";
        $chdata['created'] = isset($data['order_start_time']) ? $data['order_start_time'] : "";
        $chdata['pay_time'] = isset($data['order_end_time']) ? $data['order_end_time'] : "";
        $chdata['modified'] = isset($data['modified']) ? $data['modified'] : "";
        $chdata['buyer_nick'] = isset($data['pin']) ? $data['pin'] : "";
        $chdata['seller_flag'] = isset($data['id_remark']) ? $data['id_remark'] : '';
        $chdata['buyer_message'] = isset($data['order_remark']) ? $data['order_remark'] : "";
        $chdata['buyer_memo'] = isset($data['order_remark']) ? $data['order_remark'] : "";
        $chdata['post_fee'] = isset($data['freight_price']) ? $data['freight_price'] : "";
        $chdata['total_fee'] = isset($data['order_total_price']) ? $data['order_total_price'] : "";
        $chdata['payment'] = isset($data['order_payment']) ? $data['order_payment'] : "";
        $chdata['seller_memo'] = isset($data['vender_remark']) ? $data['vender_remark'] : "";
        $chdata['receiver_name'] = isset($data['consignee_info']['fullname']) ? $data['consignee_info']['fullname'] : '';
        $chdata['receiver_province'] = isset($data['consignee_info']['province']) ? $data['consignee_info']['province'] : '';
        $chdata['receiver_city'] = isset($data['consignee_info']['city']) ? $data['consignee_info']['city'] : '';
        $chdata['receiver_town'] = isset($data['consignee_info']['county']) ? $data['consignee_info']['county'] : '';
        $chdata['receiver_address'] = isset($data['consignee_info']['full_address']) ? $data['consignee_info']['full_address'] : '';
        $chdata['receiver_mobile'] = isset($data['consignee_info']['mobile']) ? $data['consignee_info']['mobile'] : '';
        $chdata['receiver_phone'] = isset($data['consignee_info']['telephone']) ? $data['consignee_info']['telephone'] : '';
        $chdata['seller_id'] = isset($data['vender_id']) ? $data['vender_id'] : "";
        if (isset($data['deadline'])) {
            $chdata['deadline'] = $data['deadline'];
        }
        $chdata['channel_id'] = ordermodel::JD_CHANNEL_ID; //京东渠道
        switch ($chdata['status']) {
            case "WAIT_SELLER_STOCK_OUT":
                $chdata['status'] = 1; //等待发货
                break;
            case "WAIT_GOODS_RECEIVE_CONFIRM":
                $chdata['status'] = 2; //等待确认收货
                break;
            case "FINISHED_L":
                $chdata['status'] = 3; //交易成功
                break;
            case "TRADE_CANCELED":
                $chdata['status'] = 4; //订单取消
                break;
        }


        $est = $this->getOrders($chdata['order_id']);
        if ($est) {
            $this->db->where('order_id', $chdata['order_id']);
            return $this->update(self::$table, $chdata);
        } else {
            return $this->insert(self::$table, $chdata);
        }
    }

    public function newtbOrderInfo($seller_id, $data) {
        if (!$data || !is_array($data)) {
            return null;
        }
        $chdata['order_id'] = $data['tid'];
        $chdata['status'] = isset($data['status']) ? $data['status'] : "";
        $chdata['created'] = isset($data['created']) ? $data['created'] : "";
        $chdata['pay_time'] = isset($data['pay_time']) ? $data['pay_time'] : "";
        $chdata['modified'] = isset($data['modified']) ? $data['modified'] : "";
        $chdata['buyer_nick'] = isset($data['buyer_nick']) ? $data['buyer_nick'] : "";
        $chdata['seller_flag'] = isset($data['seller_flag']) ? $data['seller_flag'] : '';
        $chdata['buyer_message'] = isset($data['buyer_message']) ? $data['buyer_message'] : "";
        $chdata['buyer_memo'] = isset($data['buyer_memo']) ? $data['buyer_memo'] : "";
        $chdata['post_fee'] = isset($data['post_fee']) ? $data['post_fee'] : "";
        $chdata['total_fee'] = isset($data['total_fee']) ? $data['total_fee'] : "";
        $chdata['payment'] = isset($data['payment']) ? $data['payment'] : "";
        $chdata['seller_memo'] = isset($data['seller_memo']) ? $data['seller_memo'] : "";
        $chdata['receiver_name'] = isset($data['receiver_name']) ? $data['receiver_name'] : '';
        $chdata['receiver_province'] = isset($data['receiver_state']) ? $data['receiver_state'] : '';
        $chdata['receiver_city'] = isset($data['receiver_city']) ? $data['receiver_city'] : '';
        $chdata['receiver_town'] = isset($data['receiver_district']) ? $data['receiver_district'] : '';
        $chdata['receiver_address'] = isset($data['receiver_address']) ? $data['receiver_address'] : '';
        $chdata['receiver_mobile'] = isset($data['receiver_mobile']) ? $data['receiver_mobile'] : '';
        $chdata['receiver_phone'] = isset($data['receiver_phone']) ? $data['receiver_phone'] : '';
        if (isset($data['deadline'])) {
            $chdata['deadline'] = $data['deadline'];
        }
        $chdata['seller_id'] = $seller_id;
        $chdata['channel_id'] = ordermodel::TAOBAO_CHANNEL_ID; //淘宝渠道
        switch ($chdata['status']) {
            case "WAIT_SELLER_SEND_GOODS":
                $chdata['status'] = 1; //等待发货
                break;
            case "WAIT_BUYER_CONFIRM_GOODS":
                $chdata['status'] = 2; //等待确认收货
                break;
            case "TRADE_FINISHED":
                $chdata['status'] = 3; //交易成功
                break;
            case "TRADE_CLOSED":
                $chdata['status'] = 4; //订单取消
                break;
        }


        $est = $this->getOrders($chdata['order_id']);
        if ($est) {
            $this->db->where('order_id', $chdata['order_id']);
            return $this->update(self::$table, $chdata);
        } else {
            return $this->insert(self::$table, $chdata);
        }
    }

    /**
     * 根据order_id列表读取订单信息
     *
     * @return mixed
     */
    public function getOrders($order_id) {
        if (!$order_id || !is_numeric($order_id)) {
            return NULL;
        }
        $this->db->select('*');
        $this->db->where('order_id', $order_id);
        return $this->get(self::$table)->result();
    }

}
